#!/usr/bin/env python3
"""
快速测试401错误原因
"""

import os
import sys
import django

# 添加backend目录到Python路径
backend_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'backend')
sys.path.insert(0, backend_path)

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'HopeEngine.settings')
django.setup()

from accounts.models import User
from django.contrib.auth import authenticate

def quick_test():
    """快速测试"""
    print("=== 快速测试401错误原因 ===")
    
    # 获取第一个用户
    user = User.objects.first()
    if not user:
        print("❌ 没有用户")
        return
    
    print(f"测试用户: {user.username}")
    print(f"用户状态: {user.status}")
    print(f"是否激活: {user.is_active}")
    
    # 测试authenticate
    print(f"\n=== 测试authenticate ===")
    auth_user = authenticate(username=user.username, password='test')
    print(f"authenticate结果: {auth_user}")
    
    if auth_user is None:
        print("❌ authenticate返回None - 用户名或密码错误")
    elif auth_user.status != 1:
        print(f"❌ 用户状态不是1: {auth_user.status}")
    else:
        print("✅ authenticate成功且状态正常")
    
    # 测试密码
    print(f"\n=== 测试密码 ===")
    test_passwords = ['test', '123456', 'password', user.username]
    for pwd in test_passwords:
        if user.check_password(pwd):
            print(f"✅ 正确密码: {pwd}")
            break
    else:
        print("❌ 所有测试密码都错误")

if __name__ == "__main__":
    quick_test() 